package com.bishe.demo.modules.other.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.dao.ImageDao;
import com.bishe.demo.modules.other.dao.SceneryImageDao;
import com.bishe.demo.modules.other.dao.TravelImageDao;
import com.bishe.demo.modules.other.entity.Image;
import com.bishe.demo.modules.other.service.ImageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private ImageDao imageDao;

    @Autowired
    private TravelImageDao travelImageDao;

    @Autowired
    private SceneryImageDao sceneryImageDao;

    @Override
    @Transactional
    public ResultEntity<Image> insertImage(Image image) {
        imageDao.insertImage(image);
        return new ResultEntity<Image>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", image);
    }

    @Override
    @Transactional
    public ResultEntity<Image> deleteImageByImageId(int imageId) {
        imageDao.deleteImageByImageId(imageId);
        travelImageDao.deleteTravelImageByImageId(imageId);
        sceneryImageDao.deleteSceneryImageByImageId(imageId);
        return new ResultEntity<Image>(ResultEntity.ResultStatus.SUCCESS.code, "删除成功");
    }

    @Override
    public Image getImageByImageId(int imageId) {
        return imageDao.getImageByImageId(imageId);
    }

    @Override
    public List<Image> getImagesBySceneryId(int sceneryId) {
        return imageDao.getImagesBySceneryId(sceneryId);
    }

    @Override
    public List<Image> getImageByTravelId(int travelId) {
        return imageDao.getImageByTravelId(travelId);
    }

    @Override
    public PageInfo<Image> getImagesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Image>(Optional
                .ofNullable(imageDao.getImagesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
